package de.cms.kff.ctrl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.cms.kff.dao.AutoDao;
import de.cms.kff.dao.DaoException;
import de.cms.kff.model.Auto;

@Controller
public class AutoController {
	private static Logger log = Logger.getLogger(AutoController.class);
	private AutoDao autoDao = null;

    public void setAutoDao(AutoDao autoDao) {
		this.autoDao = autoDao;
	}

	/**
     * <p>Boot form request.</p>
     * 
     * <p>Expected HTTP GET and request '/EditBoot.html'.</p>
     * @return 
     */
    @RequestMapping(value="/EditAuto.html", method=RequestMethod.GET)
    public ModelAndView edit(@RequestParam(required=false) Integer id) {
    	log.debug("EditAuto.html");
    	ModelAndView mv = new ModelAndView();
    	if (id == null) {
    		mv.addObject(new Auto());
    	} else { 
    		mv.addObject(autoDao.findById(id));
    	}
    	mv.setViewName("edit-auto");
    	return mv;
    }

    /**
     * <p>Saves a Boot.</p>
     * 
     * <p>Expected HTTP POST and request '/SaveBoot.html'.</p>
     * @return 
     */
    @RequestMapping(value="/SaveAuto.html", method=RequestMethod.POST)
    public String save(Auto auto, Model model) {
        if (auto.getCreated() == null) {
            auto.setCreated(new Date());
        }
        Auto result = autoDao.save(auto);
        // set id from create
        if (auto.getId() == null) {
            auto.setId(result.getId());
        }
        model.addAttribute("statusMessageKey", "mitarbeiter.form.msg.success");
        return "redirect:AlleAutos.html";
    }

    /**
     * <p>Deletes a Boot.</p>
     * 
     * <p>Expected HTTP POST and request '/LoescheBoot.html'.</p>
     */
    @RequestMapping(value="/LoescheAuto.html", method=RequestMethod.GET)
    public ModelAndView delete(Auto auto) {
        try {
			autoDao.delete(auto);
	        return new ModelAndView("redirect:AlleAutos.html");
		} catch (DaoException e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			return mv;
		}
    }

    /**
     * <p>Searches for all boats and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/AlleBoote.html'.</p>
     */
    @RequestMapping(value="/AlleAutos.html", method=RequestMethod.GET)
    public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Auto");
		mv.addObject("message", "Folgende Autos sind im Fuhrpark vorhanden");
		mv.addObject("autos", autoDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("list-autos");
		return mv;
    }

    /**
     * <p>Searches for all free foats and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/FreieBoote.html'.</p>
     */
    @RequestMapping(value="/FreieAutos.html", method=RequestMethod.GET)
    public ModelAndView freieAutos() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Verleih beginnen");
		mv.addObject("message", "Folgende Autos stehen für den Verleih zur Auswahl. Bitte wählen Sie eine Möglichkeit aus:");
		mv.addObject("autos", autoDao.findFreie());
		mv.addObject("edit", false);
		mv.setViewName("list-autos");
		return mv;
    }

}
